/*
 * @(#)ChangeNamePieItemLabelGenerator.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved. 
 * This software is the confidential and proprietary information of DCIVision Ltd ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.core;

import java.io.Serializable;
import java.text.MessageFormat;
import java.text.NumberFormat;

import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.PieDataset;

/**
 * ChangeNamePieItemLabelGenerator.java 
 * 
 * this class is overlay  the realization of JFreeChart,solve the long name in 0.921 editions can't show normally
 * 
 * @author greatwall shao
 * @company DCIVision Limited
 * @creation date 08/04/2006
 */

public class ChangeNamePieItemLabelGenerator extends StandardPieItemLabelGenerator implements Cloneable, Serializable {


  private static final int  LABLEMAXLENGTH   = 10;

  public ChangeNamePieItemLabelGenerator() {
    super();
  }

  public ChangeNamePieItemLabelGenerator(String labelFormat, NumberFormat numberFormat, NumberFormat percentFormat) {
    super(labelFormat, numberFormat, percentFormat);
  }

  public ChangeNamePieItemLabelGenerator(String labelFormat) {
    super(labelFormat);
  }

  /**
   * Returns an independent copy of the generator.
   * 
   * @return A clone.
   * @throws CloneNotSupportedException
   *           should not happen.
   */
  public Object clone() throws CloneNotSupportedException {

    ChangeNamePieItemLabelGenerator clone = (ChangeNamePieItemLabelGenerator) super.clone();

    return clone;

  }

  /**
   * Creates the array of items that can be passed to the {@link MessageFormat} class for creating labels.
   * 
   * @param dataset
   *          the dataset (<code>null</code> not permitted).
   * @param key
   *          the key.
   * @return The items (never <code>null</code>).
   */
  protected Object[] createItemArray(PieDataset dataset, Comparable key) {
    Object[] result = new Object[3];
    result[0] = key.toString().length() > getLableLength() ? key.toString().substring(0, getLableLength()) + "..." : key.toString();
    Number value = dataset.getValue(key);
    result[1] = this.getNumberFormat().format(value);
    double percent = 0.0;
    if (value != null) {
      double v = value.doubleValue();
      if (v > 0.0) {
        percent = v / DatasetUtilities.calculatePieDatasetTotal(dataset);
      }
    }
    result[2] = this.getPercentFormat().format(percent);
    return result;
  }

  private int getLableLength() {
    return LABLEMAXLENGTH;
  }

}
